//
//  SafePrivacyVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/6.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "SafePrivacyVC.h"
#import "MicroDef.h"
#import "FACEViewController.h"
#import "TextSwitchCell.h"
#import "SettingTextCell.h"
#import "ModifyPasswordVC.h"
#import "CUserSettingModel.h"
#import "CUserSettingMgr.h"
#import "CXmppRequestMgr.h"
#import "GuestureController.h"
//#import "CAccountMgr.h"
//#import "CAccountModel.h"
#import "fingerPrintViewController.h"
#import "CFolderMgr.h"
#import <LocalAuthentication/LocalAuthentication.h>
#define KSECTIONHEIGHT 20.0f
@interface SafePrivacyVC ()<UITableViewDelegate, UITableViewDataSource, TextSwitchCellDelegate>
{
    UITableView *_tableView;
}

@property (nonatomic, strong) CUserSettingModel *setting;
@property (nonatomic) BOOL finger;
@end

@implementation SafePrivacyVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPSETNOTICESETTING object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    LAContext *context = [[LAContext alloc]init];
    BOOL ret = [context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil];
    if (ret) {
        _finger = YES;
    }
    else
    {
        _finger = NO;
    }
    // Do any additional setup after loading the view.
    CUserSettingMgr *setMgr = [[CUserSettingMgr alloc] init];
    self.setting = [setMgr getUserSetting];
    if (self.setting == nil) {
        self.setting = [[CUserSettingModel alloc] init];
        self.setting.userCode = [[CFolderMgr shareInstance].accMgr getRecentlyLoginAccount].userName;
        [setMgr insetUserWithSetting:self.setting];
    }
    self.navigationItem.title = @"安全与隐私";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSetting) name:K_XMPPSETNOTICESETTING object:nil];
    
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = self.view.frame;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSInteger rows = 0;
    
    switch (section) {
        case 0:
        {
                if (_finger) {
                    rows = 5;
                }
                else{
                    rows = 4;
                }
        }
            break;
        default:
            rows = 0;
            break;
    }
    return rows;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    return 4;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger count = [tableView numberOfRowsInSection:indexPath.section];
    static NSString *CellIdentifier = @"TextSwitchCell";
    static NSString *TextCellIdentifier = @"SettingTextCell";
    //    DLog(@"section:%d, rows:%d", indexPath.section, indexPath.row);
    UITableViewCell *cell = nil;
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        TextSwitchCell  *textSwtichCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (textSwtichCell == nil) {
            textSwtichCell = [[[NSBundle mainBundle] loadNibNamed:@"TextSwitchCell" owner:self options:nil] objectAtIndex:0];
        }
        [textSwtichCell.seperatorLine setHidden:NO];
        textSwtichCell.delegate = self;
        textSwtichCell.switchController.on = self.setting.BothOnline;
        textSwtichCell.indexPath = indexPath;
        [textSwtichCell.titleLabel setText:[self titleName:indexPath]];
        cell = textSwtichCell;
    }
    else
    {
        SettingTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
        if (textCell == nil) {
            textCell = [[[NSBundle mainBundle] loadNibNamed:@"SettingTextCell" owner:self options:nil] objectAtIndex:0];
        }
        if(indexPath.section == 0 &&  indexPath.row != count -1)
        {
            [textCell.seperatorLine setHidden:NO];
        }
        else
        {
            [textCell.seperatorLine setHidden:YES];
        }
        [textCell.titleLabel setText:[self titleName:indexPath]];
        cell = textCell;
    }
    
    return cell;
}

- (void)SwitchCellDidChangeSettingWithIndexPath:(NSIndexPath *)indexPath settingValue:(BOOL)settingValue
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    self.setting.BothOnline = settingValue;
    
    if (self.setting.Voice == 1) {
        dict[@"sound"] = @"yes";
    } else {
        dict[@"sound"] = @"no";
    }
    if (self.setting.Shake == 1) {
        dict[@"vibration"] = @"yes";
    } else {
        dict[@"vibration"] = @"no";
    }
    if (self.setting.Disturb == 1) {
        dict[@"noDisturb"] = @"yes";
        dict[@"noDisturbStart"] = @"23:00";
        dict[@"noDisturbEnd"] = @"8:00";
        
    } else {
        dict[@"noDisturb"] = @"no";
        dict[@"noDisturbStart"] = @"";
        dict[@"noDisturbEnd"] = @"";
    }
    if (self.setting.BothOnline == 1) {
        dict[@"sameOnline"] = @"yes";
    } else {
        dict[@"sameOnline"] = @"no";
    }
    [[CXmppRequestMgr shareInstance] SetUserSettingInfoWithSettingStr:[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]];
}


- (void)saveSetting
{
    
    [[[CUserSettingMgr alloc] init] updateUserSetting:self.setting];
    [_tableView reloadData];
}


-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    title = @"允许手机,电脑同时在线";
                    break;
                case 1:
                    title = @"手势锁定密码";
                    break;
                case 2:
                    title = @"修改登录密码";
                    break;
                case 3:
                    title = @"人脸识别";
                    break;
                case 4:
                    title = @"指纹支付";
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return title;
}




#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    CGFloat height = 0;
    switch (section) {
        case 0:
            height = 0;
            break;
        default:
            break;
    }
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0;
    switch (section) {
        case 0:
            height = KSECTIONHEIGHT;
            break;
        default:
            break;
    }
    return height;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
{
    UIView *headerView = nil;
    //    if(section == 0)
    //    {
    //        return headerView;
    //    }
    //    headerView = [[UIView alloc] init];
    //    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
    //    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    //    UIView *beginLine = [[UIView alloc] init];
    //    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    //    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    //    [headerView addSubview:beginLine];
    //
    //    UIView *endLine = [[UIView alloc] init];
    //    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
    //    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
    //    [headerView addSubview:endLine];
    
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
{
    UIView *headerView = nil;
    if(section != 0)
    {
        return headerView;
    }
    headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 3);
    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
    UIView *beginLine = [[UIView alloc] init];
    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
    [headerView addSubview:beginLine];
    
    
    
    return headerView;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 50.0f;
    
    if(indexPath.row == 0 && indexPath.section == 0)
    {
        height = 50.0f;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                {
                    [self showGuestureController];
                }
                    break;
                case 2:
                {
                    [self showModifyPasswordVC];
                }
                    break;
                case 3:
                {
                    [self showFaceVC];
                }
                    break;
                case 4:
                {
                    [self showfingerVC];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    
    
}
-(void)showfingerVC
{
    fingerPrintViewController *vc = [[fingerPrintViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)showGuestureController
{
    GuestureController *vc = [[GuestureController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)showModifyPasswordVC
{
    ModifyPasswordVC *modifyPassword = [[ModifyPasswordVC alloc] init];
    //    CHSNavigationController *modifyPasswordNav = [[CHSNavigationController alloc] initWithRootViewController:modifyPassword];
    //    [self.navigationController presentViewController:messageSettingNav animated:YES completion:nil];
    [self.navigationController pushViewController:modifyPassword animated:YES];
    
}
-(void)showFaceVC
{
    FACEViewController *facevc = [[FACEViewController alloc]init];
    [self.navigationController pushViewController:facevc animated:YES];
}

@end
